package yhao.infra.feature.qiniu.oss.core;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import yhao.infra.common.util.CommonListUtil;
import yhao.infra.feature.qiniu.oss.QiniuOssProperties;
import yhao.infra.feature.qiniu.oss.type.QiniuImgType;
import yhao.infra.feature.qiniu.oss.type.QiniuImgTypeHelper;
import yhao.infra.feature.qiniu.oss.type.QiniuImgTypeParam;

import jakarta.annotation.PostConstruct;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 七牛文件上传
 */
public class QiniuOssComponent {
    @Autowired
    private QiniuOssProperties qiniuProperties;

    private BucketInfo protectBucket;
    private BucketInfo fileBucket;

    protected QiniuOssProperties getQiniuProperties() {
        return qiniuProperties;
    }

    protected BucketInfo getProtectBucket() {
        return protectBucket;
    }

    protected BucketInfo getFileBucket() {
        return fileBucket;
    }

    public String getBucketPrefix(boolean protect){
        if(protect){
            return protectBucket.getPrefix();
        }
        return fileBucket.getPrefix();
    }

    public String getBucketUrl(boolean protect){
        if(protect){
            return protectBucket.getUrl();
        }
        return fileBucket.getUrl();
    }

    @PostConstruct
    private void init() {
        if (StringUtils.isBlank(qiniuProperties.getAccessKey())) {
            throw new RuntimeException("七牛图片组件需要配置accessKey");
        }
        if (StringUtils.isBlank(qiniuProperties.getSecretKey())) {
            throw new RuntimeException("七牛图片组件需要配置secretKey");
        }
        fileBucket = new BucketInfo(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey(),
                qiniuProperties.getFile().getName(),qiniuProperties.getFile().getUrl(),
                qiniuProperties.getFile().getZone());
        if(StringUtils.isNotBlank(qiniuProperties.getFile().getPrefix())){
            fileBucket.setPrefix(qiniuProperties.getFile().getPrefix());
        }
        if(qiniuProperties.isProtectUseFileProperties()){
            protectBucket = fileBucket;
        }else{
            protectBucket = new BucketInfo(qiniuProperties.getAccessKey(), qiniuProperties.getSecretKey(),
                    qiniuProperties.getProtect().getName(),qiniuProperties.getProtect().getUrl(),
                    qiniuProperties.getProtect().getZone());
            if(StringUtils.isNotBlank(qiniuProperties.getProtect().getPrefix())){
                protectBucket.setPrefix(qiniuProperties.getProtect().getPrefix());
            }
        }
    }

    /**
     * 获取水印图片的原始连接
     * <p>仅上传时设置了水印参数的图片有效</p>
     *
     * @param url
     * @param expireInSeconds 失效时间（单位s），若为null则默认半个小时,最多设置2小时
     * @return
     */
    public String getOriginalUrl(String url,Integer expireInSeconds) {
        long seconds = 1800L;
        if(expireInSeconds!=null){
            if(expireInSeconds>7200){
                seconds = 7200L;
            }else{
                seconds = expireInSeconds;
            }
        }
        String finalUrl = protectBucket.getAuth().privateDownloadUrl(url, seconds);
        return finalUrl;
    }

    /**
     * 下载文件
     */
    public void download(HttpServletRequest request, HttpServletResponse response,
                         String url, String fileName) throws IOException {
        DownloadUtil.downloadAttachment(request, response, url, fileName);
    }

    /**
     * 下载文件
     */
    public byte[] download(String url) throws IOException {
        return DownloadUtil.downloadAttachment(url);
    }

    /**
     * url转换
     * @param urlList 待转换的url
     * @param param {@link QiniuImgTypeParam}图片样式参数
     * @return
     */
    public List<String> addUrlPostfix(List<String> urlList, QiniuImgTypeParam param){
        if(CommonListUtil.isEmpty(urlList)){
            return urlList;
        }
        List<String> postfix = new ArrayList<>(urlList.size()*2);
        QiniuImgType type = QiniuImgTypeHelper.selectType(param);
        for(String url:urlList){
            postfix.add(type.makeUrl(url));
        }
        return postfix;
    }

}
